package com.hxy.controller;

import com.hxy.entity.Type;
import com.hxy.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

// 允许跨域请求
@CrossOrigin
@RestController
@RequestMapping("type")
public class TypeController {

    @Resource
    TypeService typeService;

    @RequestMapping("list")
    public PageInfo list(Integer pageNum,Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        // ServiceImpl实现的方法
        List<Type> list = typeService.list();
        PageInfo<Type> typePageInfo = new PageInfo<>(list);
        return typePageInfo;
    }

    @RequestMapping("del")
    public String del(Integer tid) {
        typeService.removeById(tid);
        return "redirect:list";
    }

    @RequestMapping("save")
    public void save(Type type) {
        typeService.save(type);
    }

    @RequestMapping("update")
    public void update(Type type) {
        typeService.updateById(type);
    }


    // 保存或者修改，先根据条件查询，查到数据做修改操作，查不到数据做添加操作
    @RequestMapping("saveOrUpdate")
    public void saveOrUpdate(Type type) {
        typeService.saveOrUpdate(type);// 默认按主键查询
        // typeService.saveOrUpdate(type,wrapper);// 查询条件
    }
}
